package org.karelun.gef.utils;

import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Display;

public class FontDataUtils {
	
	public static Dimension getFontSize(String text,FontData data){
		if(data==null)
			return null;
		Display display = Display.getCurrent();
		Font font = new Font(display,data);
		GC gc = new GC(display);
		gc.setFont(font);
		Point extent = gc.textExtent(text);
		extent.x+=20;
		extent.y+=5;
		return new Dimension(extent);
		
//		data.getHeight();
//		Display display = Display.getCurrent();
//		GC gc = new GC(display);
//		gc.setFont(new Font(display,data));
//		FontMetrics metrics = gc.getFontMetrics();
//		int as = metrics.getAscent();
//		int d = metrics.getDescent();
//		int h = metrics.getHeight();
//		int w = metrics.getAverageCharWidth();
//		int l = metrics.getLeading();
//		int length = text.length();
//		return new Dimension((w+l)*length,as+h+d);
	}
	
	public static String toValue(FontData fontData) {
		String name = fontData.getName();
		int height = fontData.getHeight();
		int style = fontData.getStyle();
		return "Font {" + name + " , " + String.valueOf(height) + " , " + toString(style) + "}"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
	}

	private static String toString(int style) {
		String ret = ""; //$NON-NLS-1$
		if ((style & SWT.BOLD) != 0)
			ret += "BOLD"; //$NON-NLS-1$
		if ((style & SWT.ITALIC) != 0)
			ret += ", ITALIC"; //$NON-NLS-1$
		if (ret.startsWith(",")) //$NON-NLS-1$
			return ret.substring(1);
		return ret;
	}
}
